package com.goods.shop.util;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.goods.shop.service.constants.WeiXinUrlFinals;
import com.goods.shop.service.model.ValueColorModel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

public class WeiXinMessageUtils {
    public static Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
    //设置的行业信息
    public static String setIndustry(String accessToken){
        String url= WeiXinUrlFinals.SETINDUSTRYURL.replaceAll("\\{"+WeiXinUrlFinals.ACCESSTOKEN+"\\}",accessToken);
        String result="";
        Map<String,Object> params=new HashMap<>();
        params.put("industry_id1","4");
        params.put("industry_id2","5");
        String paramsJson= JSONObject.toJSONString(params);
        try {
            //获取微信callback IP地址
            result= HttpRequest.post(url).body(paramsJson,"application/json").execute().body().toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    //获取设置的行业信息
    public static String getIndustry(String accessToken){
        String url= WeiXinUrlFinals.GETINDUSTRYURL.replaceAll("\\{"+WeiXinUrlFinals.ACCESSTOKEN+"\\}",accessToken);
        String result="";
        try {
            //获取微信callback IP地址
            result= HttpRequest.get(url).execute().body().toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    //获取模板列表
    public static String getAllTemplates(String accessToken){
        String url= WeiXinUrlFinals.GETALLTEMPLATESURL.replaceAll("\\{"+WeiXinUrlFinals.ACCESSTOKEN+"\\}",accessToken);
        String result="";
        try {
            //获取微信callback IP地址
            result= HttpRequest.get(url).execute().body().toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    //获取模板列表

    /**
     *
     * @param accessToken
     * @param toUser   用户的ID
     * @param smsTemplateId  模板的ID
     * @return
     */
    @Deprecated
    public static String sendMessage(String accessToken,String toUser,String smsTemplateId,String message){
        String url= WeiXinUrlFinals.SENDMESSAGEURL.replaceAll("\\{"+WeiXinUrlFinals.ACCESSTOKEN+"\\}",accessToken);
        String result="";
        Map<String,Object> params=new HashMap<>();
        params.put("touser",toUser);//"oquGis5b4qIVfEE1XHeBiuDa6ZJU"
        params.put("template_id",smsTemplateId);//"WeVJtJ-22o7wb6SNPtT62kpFPgFc2rvTPEV-8lca0x0"
        params.put("url","https://www.jd.com");//TODO
        Map<String,Object> datas=new HashMap<>();
//        datas.put("count",new ValueColorModel("100","#FF0000"));
//        datas.put("dateTime",new ValueColorModel(LocalDateTime.now().toString(),"#FF0000"));
        datas.put("message",new ValueColorModel(message,"#000000"));

        params.put("data",datas);
        String jsonParams=JSONObject.toJSONString(params);
        logger.info("jsonParams="+jsonParams);
        try {
            //获取微信callback IP地址
            result= HttpRequest.post(url).body(jsonParams,"application/json").execute().body().toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    public static String sendMessage(String accessToken,String toUser,String smsTemplateId,String message,String detailUrl){
        String url= WeiXinUrlFinals.SENDMESSAGEURL.replaceAll("\\{"+WeiXinUrlFinals.ACCESSTOKEN+"\\}",accessToken);
        String result="";
        Map<String,Object> params=new HashMap<>();
        params.put("touser",toUser);//"oquGis5b4qIVfEE1XHeBiuDa6ZJU"
        params.put("template_id",smsTemplateId);//"WeVJtJ-22o7wb6SNPtT62kpFPgFc2rvTPEV-8lca0x0"
        params.put("url",detailUrl);//TODO---点击详情跳转的页面
        Map<String,Object> datas=new HashMap<>();
//        datas.put("count",new ValueColorModel("100","#FF0000"));
//        datas.put("dateTime",new ValueColorModel(LocalDateTime.now().toString(),"#FF0000"));
        datas.put("message",new ValueColorModel(message,"#000000"));

        params.put("data",datas);
        String jsonParams=JSONObject.toJSONString(params);
        logger.info("jsonParams="+jsonParams);
        try {
            //获取微信callback IP地址
            result= HttpRequest.post(url).body(jsonParams,"application/json").execute().body().toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    public static void main(String[] args) {
        String appidTest="wxce55f7ed02ae4e66";
        String secretTest="f3ebf525a742e8d04ecaa24451e0e999";
        String toUser="oquGis7BLdB5Yq_FcUm4NpQEafEY";
        String templateId="WeVJtJ-22o7wb6SNPtT62kpFPgFc2rvTPEV-8lca0x0";
        for(int i=0;i<10;i++){
            String accessToken= WeiXinUtils.getAccessToken(appidTest,secretTest);
            System.out.println(accessToken);
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
//        logger.info("accessToken="+accessToken);
//        String result="";
////        result=WeiXinMessageUtils.setIndustry(accessToken);
////        logger.info("setIndustry.result={}",result);
////        result=WeiXinMessageUtils.getIndustry(accessToken);
////        logger.info("getIndustry.result={}",result);
//        result=WeiXinMessageUtils.getAllTemplates(accessToken);
//        logger.info("getAllTemplates.result={}",result);
//        result=WeiXinMessageUtils.sendMessage(accessToken,toUser,templateId);
//        logger.info("sendMessage.result={}",result);

    }
}
